<template>
	<view class="receivingAddress">
		<uninavbar title="我的收货地址" leftIcon='leftIcon' rightIcon='' rightText="" />
		<view class="AddressCon">


			<view v-if="addressListData.length>0" class="addressList" v-for="(item,index) in addressListData"
				:key="index">
				<view class="addressLeft">
					{{item.name[0]}}
				</view>
				<view class="addressRight">
					<view class="addressInfo">

						<view class="" @tap.stop="goBack(item)">
							<view class="">
								{{item.name}} <text>{{item.phoneNumber}}</text> 
							</view>
							<view class="addressText">
								{{item.province}}-{{item.city}}-{{item.region}} {{item.detailAddress}}
							</view>

						</view>
						<navigator class="item" :url="'/pages/user/newAddress?id='+item.id">
							<view class="">
								<image src="/static/images/user/bi.png" mode=""></image>
							</view>
						</navigator>
					</view>

					<view class="addressSelected">
						<view class="selectedBtn" @click.stop="selectedDom(item,index)">
							<image v-if="item.defaultStatus" src="/static/images/productDetails/show.png"></image>
							<view class="selectedHide" v-else>

							</view>
							<text>默认地址</text>
						</view>
						<view class="" @tap.stop="addressDelete(item)">
							删除
						</view>

					</view>
				</view>
			</view>


			<view class="addressListNull" v-if="addressListData.length==0">
				<image src="/static/images/user/dz.png" mode="widthFix"></image>
				<view class="">
					暂无收货地址
				</view>
			</view>
			<view class="addressfoot">
				<navigator class="item" url="/pages/user/newAddress">
					<view class="addressBtn">
						+ 添加收货地址
					</view>
				</navigator>
			</view>

		</view>
	</view>
</template>

<script>
	import uninavbar from '@/components/uni-nav-bar/uni-nav-bar.vue';
	import API from '@/config/newApi.js'
	export default {
		components: {
			uninavbar,
		},
		data() {
			return {
				addressListData: [],
				goUrltype: ''
			}
		},
		onShow() {
			this.addressList()
		},
		onLoad(data) {
			if (data.goUrl) {
				this.goUrltype = data.goUrl
			}
		},
		methods: {
			goBack(item) {
				if (this.goUrltype) {
					uni.$emit('newAddPv', item)
					uni.navigateBack({
						delta: 1
					});
				}
			},
			addressDelete(item) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '确定删除地址？',
					success: function(res) {
						if (res.confirm) {
							API.addressDelete(item.id).then(res => {
									if (res.code == 200) {
										that.$alert(res.message);
										that.addressList()
									}
								})
								.catch(err => {
									// this.$alert(res.message);
								})
						} else if (res.cancel) {
							// 用户取消，可以选择不执行任何操作或者执行其他操作
						}
					}
				});

			},
			addressUpdate(item) {

				let data = item
				let args = {
					ContentType: 'application/json'
				}
				API.addressUpdate(data, args).then(res => {
						if (res.code == 200) {
							this.$alert(res.message);
						}
					})
					.catch(err => {
						this.$alert(res.message);
					})
			},
			addressList() {
				API.addressList().then(res => {
						if (res.code == 200) {
							this.addressListData = res.data
						}
					})
					.catch(err => {
						this.$alert(res.message);
					})
			},
			selectedDom(item, index) {
				for (let i of this.addressListData) {
					i.defaultStatus = 0
				}
				this.addressListData[index].defaultStatus = 1
				this.addressUpdate(item)
			}
		}
	}
</script>

<style scoped lang="scss">
	.receivingAddress {
		padding-top: 90rpx;
		.AddressCon{
			height: calc(100vh - 250rpx);
			overflow: auto;
			padding-bottom: 140rpx;
		}
		.addressList {
			background-color: #fff;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 40rpx;
			padding-bottom: 20rpx;
			border-radius: 16rpx;
			margin-bottom: 20rpx;

			.addressLeft {
				width: 70rpx;
				height: 70rpx;
				background: #F3F2FC;
				color: #6954D5;
				border-radius: 50%;
				text-align: center;
				line-height: 70rpx;
			}

			.addressRight {
				flex: 1;
				margin-left: 40rpx;
			}

			.addressInfo {
				display: flex;
				align-items: center;
				justify-content: space-between;
				flex: 1;
				text{
					color: #999;
					font-size: 20rpx;
					
				}

			}

			.addressSelected {
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: #999;
				border-top: 1px solid rgba(221, 221, 221, 0.87);
				margin-top: 20rpx;
				padding-top: 20rpx;
				font-size: 24rpx;

				.selectedBtn {
					display: flex;
					align-items: center;

					.selectedHide {
						width: 30rpx;
						height: 30rpx;
						border: 2rpx solid #f1f1f1;
						border-radius: 50%;
						margin-right: 10rpx;
					}
				}

				image {
					width: 35rpx;
					height: 35rpx;
					margin-right: 10rpx;
				}

			}

			.addressText {
				color: #999;
				width: 380rpx;
				font-size: 20rpx;
			}

			image {
				width: 50rpx;
				height: 50rpx;
			}
		}

		.addressfoot {
			width: 100%;
			position: fixed;
			bottom: 0;
			max-width: 800rpx;
			text-align: center;
			box-sizing: border-box;
			padding: 40rpx 0;
			background-color: #fff;
		}

		.addressBtn {
			width: 520rpx;
			height: 80rpx;
			margin: auto;
			background: #6954D5;
			border-radius: 40rpx;
			color: #fff;
			text-align: center;
			line-height: 80rpx;

		}
	}

	.addressListNull {
		text-align: center;
		color: #999;
		margin-top: 100rpx;
	}
</style>